package com.gx.smart.construction.ui.house.paging;

import android.os.AsyncTask;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.paging.PagedList;

import com.blankj.utilcode.util.ToastUtils;
import com.casic.gx.grpc.common.ComQuery;
import com.casic.gx.grpc.common.ComResp;
import com.gx.smart.construction.ui.common.db.ConstructionDatabase;
import com.gx.smart.construction.ui.house.model.RoomInfo;
import com.gx.smart.lib.http.api.ConstructionAppRoomInfoProviderService;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppGetAllRoomsInfoResponse;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppRoomInfo;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaosy
 * 时间: 2022/2/11
 * @Description: 边界回调
 * @Version: 1.0
 **/
public class RoomInfoBoundaryCallback extends PagedList.BoundaryCallback<RoomInfo> {

    private List<RoomInfo> roomInfoList;
    private int currentPage = 1;

    public RoomInfoBoundaryCallback() {
        roomInfoList = new ArrayList<>();
    }

    @Override
    public void onZeroItemsLoaded() {
        super.onZeroItemsLoaded();
        Logger.e("onZeroItemsLoaded");
        loadRoomList(currentPage, 3);
    }

    @Override
    public void onItemAtFrontLoaded(@NonNull RoomInfo itemAtFront) {
        super.onItemAtFrontLoaded(itemAtFront);
//        AsyncTask.execute(() -> ConstructionDatabase.getInstance().getRoomInfoDao().clearRoomInfo());
    }

    @Override
    public void onItemAtEndLoaded(@NonNull RoomInfo itemAtEnd) {
        super.onItemAtEndLoaded(itemAtEnd);
        Logger.e("onItemAtEndLoaded");
        currentPage = itemAtEnd.page + 1;
        loadRoomList(currentPage, 3);
    }

    private void addConversionList(List<ConstructionAppRoomInfo> sourceList,
                                   List<RoomInfo> targetList, int page) {
        if (sourceList == null || sourceList.isEmpty()) {
            return;
        }
        targetList.clear();
        for (ConstructionAppRoomInfo item :
                sourceList) {
            targetList.add(new RoomInfo(
                    item.getId(),
                    item.getZoneName(),
                    item.getBuildingName(),
                    item.getUnitName(),
                    item.getNumber(),
                    item.getSn(),
                    page
            ));
        }
    }

    private void loadRoomList(int currentPage, int type) {
        ComQuery comQuery = ComQuery.newBuilder()
                .setPage(currentPage)
                .setPageSize(15)
                .build();
        ConstructionAppRoomInfoProviderService.getInstance().getAllRoomsInfo(comQuery, type, new CallBack<ConstructionAppGetAllRoomsInfoResponse>() {

            @Override
            public void callBack(ConstructionAppGetAllRoomsInfoResponse result) {
                if (result == null) {
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    insertRoomInfo(result.getContentList(), result.getComQuery().getPage());
                } else {
                    String tip = comResp.getTip();
                    if (TextUtils.isEmpty(tip)) {
                        ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE);
                    } else {
                        ToastUtils.showLong(tip);
                    }
                }
            }
        });
    }

    private void insertRoomInfo(List<ConstructionAppRoomInfo> sourceList, int page) {
        if (sourceList == null || sourceList.isEmpty()) {
            return;
        }
        addConversionList(sourceList, roomInfoList, page);
        AsyncTask.execute(() -> ConstructionDatabase.getInstance().getRoomInfoDao().insertRoomInfoList(roomInfoList));
    }
}
